//
//  ShopCarGoodsCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/11.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "ShopCarGoodsCell.h"
#import "ShopCarController.h"

@interface ShopCarGoodsCell ()
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIButton *subBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIImageView *leftImageView;

@property (weak, nonatomic) IBOutlet UILabel *goodsDesLabel;
@property (weak, nonatomic) IBOutlet UILabel *middelLabel;

@property (weak, nonatomic) IBOutlet UILabel *goodsPriceLabel;

@property (weak, nonatomic) IBOutlet UIView *marginView;

@property (nonatomic, assign) NSInteger goodsCount;
@end

@implementation ShopCarGoodsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.leftBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    self.leftImageView.layer.borderColor = colorWithA(75, 75, 75, 0.2).CGColor;
    self.leftImageView.layer.cornerRadius = 5;
    self.leftImageView.layer.masksToBounds = YES;
    self.leftImageView.layer.borderWidth = 0.5;
    
    self.middelLabel.textColor = darkGray;
    
    self.middelLabel.font = MYFONT(10);
    
    self.bgView.layer.borderWidth = 1;
    self.bgView.layer.borderColor = [[UIColor colorWithHexString:@"dddddd"] CGColor];
    self.bgView.layer.cornerRadius = 3;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    
    
    
    [self.subBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 2, 0)];
    [self.subBtn setTitleColor:[UIColor colorWithRed:166.0/255.0 green:166.0/255.0 blue:166.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    [self.subBtn setTitleColor:[UIColor colorWithRed:196.0/255.0 green:196.0/255.0 blue:196.0/255.0 alpha:1.0] forState:UIControlStateDisabled];
    [self.subBtn setBackgroundImage:[UIImage imageWithMyColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.subBtn setBackgroundImage:[UIImage imageWithMyColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]] forState:UIControlStateDisabled];
    self.subBtn.exclusiveTouch = YES;
    self.subBtn.backgroundColor=[UIColor clearColor];
    
    
    
    self.countLabel.textAlignment=NSTextAlignmentCenter;
    self.countLabel.textColor = [UIColor colorWithHexString:@"333333"];
    self.countLabel.font = MYFONT(13);
    self.countLabel.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    [self.addBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 5, 0)];
    [self.addBtn setTitleColor:[UIColor colorWithRed:65.0/255 green:164.0/255 blue:249.0/255 alpha:1] forState:UIControlStateNormal];
    [self.addBtn setTitleColor:[UIColor colorWithRed:196.0/255.0 green:196.0/255.0 blue:196.0/255.0 alpha:1.0] forState:UIControlStateDisabled];
    [self.addBtn setBackgroundImage:[UIImage imageWithMyColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.addBtn setBackgroundImage:[UIImage imageWithMyColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]] forState:UIControlStateDisabled];
    self.addBtn.backgroundColor=[UIColor clearColor];
    self.addBtn.exclusiveTouch = YES;
    
    self.goodsDesLabel.font = MYFONT(15);
    
    self.goodsPriceLabel.font = MYFONT(13);

    // Initialization code
}

- (void)setModel:(GoodsSkuListModel *)model{
    _model = model;
    
    if (_model.count.integerValue > 1) {
        
        self.subBtn.enabled = YES;
        
    } else {
        self.subBtn.enabled = NO;
        
    }
    
    if (_model.count.integerValue >= 99) {
        self.addBtn.enabled = NO;
        
    } else {
        self.addBtn.enabled = YES;
        
    }
    
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:_model.goods_image_url]];
    
    self.goodsDesLabel.text = _model.goods_name;
    
    self.goodsPriceLabel.text = [NSString stringWithFormat:@"%@P",_model.price];
    
    self.countLabel.text = model.count;
    
    self.goodsCount = _model.count.integerValue;
    
    if (_model.middelText == nil) {
        self.middelLabel.hidden = YES;
    } else {
        self.middelLabel.hidden = NO;
        
        self.middelLabel.text = _model.middelText;

    }
    
    
    if ([_model.isSelect isEqualToString:@"1"]) {
        
        self.leftBtn.selected = YES;
        
    } else {
        
        self.leftBtn.selected = NO;
        
    }
    

    
    
}

- (IBAction)subBtnDidClick:(UIButton *)sender {
    
    --self.goodsCount;
    
    self.addBtn.enabled = YES;
    
    if (self.goodsCount == 1) {
        self.subBtn.enabled = NO;
    }
    
    self.countLabel.text=[NSString stringWithFormat:@"%zi",self.goodsCount];
    
    self.model.count = self.countLabel.text;
    
    ShopCarController *controller = (ShopCarController *)self.viewController;
    
    [controller calculateTotalPrice];
    
}

- (IBAction)addBtnDidClick:(UIButton *)sender {
    
    ++self.goodsCount;
    
    
    if (self.goodsCount > 1) {
        self.subBtn.enabled = YES;
    }
    
    if (self.goodsCount >= 99) {
        
        self.goodsCount = 99;
        sender.enabled = NO;
    }
    
    
    self.countLabel.text = [NSString stringWithFormat:@"%zi",self.goodsCount];
    
    self.model.count = self.countLabel.text;
    
    ShopCarController *controller = (ShopCarController *)self.viewController;
    
    [controller calculateTotalPrice];
    
    
}


- (IBAction)leftBtnDidClick:(UIButton *)sender {
    
    self.leftBtn.selected = !self.leftBtn.selected;
    
    if (self.leftBtn.selected) {
        
        self.model.isSelect = @"1";
        
    } else {
        
        self.model.isSelect = @"0";
        
    }
    
    ShopCarController *controller = (ShopCarController *)self.viewController;
    
    [controller calculateTotalPrice];
    
//    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:self.index.section];
    [controller.plainTableView reloadData];
    
//    [self.delegate singleClick:self.model indexPath:self.index];
}

- (IBAction)deleBtnDidClick:(UIButton *)sender {
    
    ShopCarController *controller = (ShopCarController *)self.viewController;
    
    [controller deleBtnDidClickWithIndexPath:self.index];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
